#include <gtest/gtest.h>
#include "../mqserver/mq_message.hpp"
#include "../mqcommon/mq_helper.hpp"

csjmq::MessageManager::ptr mmp;
class Messagetest : public testing::Environment
{
    virtual void SetUp() override
    {
        mmp = std::make_shared<csjmq::MessageManager>("./data/message");
        mmp->initQueueMessage("queue1");
    }

    virtual void TearDown() override
    {
        // mmp->clear();
    }
};

// 新增消息测试：新增消息，然后观察课获取消息数量，以及持久化消息数量
TEST(Messagetest, insert_test)
{

    csjmq::BasicProperties properties;
    properties.set_id(csjmq::UUIDHelper::uuid());
    properties.set_delivery_mode(csjmq::DeliveryMode::DURABLE);
    properties.set_routing_key(".root.arr");

    mmp->insert("queue1", &properties, "hello-world-1", csjmq::DeliveryMode::DURABLE);
    mmp->insert("queue1", nullptr, "hello-world-2", csjmq::DeliveryMode::DURABLE);
    mmp->insert("queue1", nullptr, "hello-world-3", csjmq::DeliveryMode::DURABLE);
    mmp->insert("queue1", nullptr, "hello-world-4", csjmq::DeliveryMode::DURABLE);
    mmp->insert("queue1", nullptr, "hello-world-5", csjmq::DeliveryMode::UNDURABLE);

    ASSERT_EQ(mmp->getable_count("queue1"), 5);
    ASSERT_EQ(mmp->durable_count("queue1"), 4);
    ASSERT_EQ(mmp->total_count("queue1"), 4);
    ASSERT_EQ(mmp->waitack_count("queue1"), 0);
}

// 获取消息测试，获取一条消息，不进行消息确认的情况下，查看可获取消息的数量，待确认消息的数量，以及测试消息的获取顺序
TEST(Messagetest, select_test)
{
    csjmq::MessagePtr mp1 = mmp->front("queue1");
    ASSERT_EQ(mp1->payload().body(), std::string("hello-world-1"));
    ASSERT_EQ(mmp->getable_count("queue1"), 3);
    ASSERT_EQ(mmp->waitack_count("queue1"), 1);

    csjmq::MessagePtr mp2 = mmp->front("queue1");
    ASSERT_EQ(mp2->payload().body(), std::string("hello-world-2"));
    ASSERT_EQ(mmp->getable_count("queue1"), 2);
    ASSERT_EQ(mmp->waitack_count("queue1"), 2);

    csjmq::MessagePtr mp3 = mmp->front("queue1");
    ASSERT_EQ(mp3->payload().body(), std::string("hello-world-3"));
    ASSERT_EQ(mmp->getable_count("queue1"), 1);
    ASSERT_EQ(mmp->waitack_count("queue1"), 3);

    csjmq::MessagePtr mp4 = mmp->front("queue1");
    ASSERT_EQ(mp4->payload().body(), std::string("hello-world-4"));
    ASSERT_EQ(mmp->getable_count("queue1"), 0);
    ASSERT_EQ(mmp->waitack_count("queue1"), 4);

    csjmq::MessagePtr mp5 = mmp->front("queue1");
    ASSERT_EQ(mp5.get(), nullptr);
}

// 删除测试，确认一条消息，查看持久化消息数量，待确认消息数量
TEST(Messagetest, delete_test)
{
    ASSERT_EQ(mmp->getable_count("queue1"), 8);
    ASSERT_EQ(mmp->waitack_count("queue1"), 0);

    csjmq::MessagePtr mp = mmp->front("queue1");
    ASSERT_EQ(mmp->getable_count("queue1"), 7);
    ASSERT_EQ(mmp->waitack_count("queue1"), 1);

    mmp->ack("queue1", mp->payload().properties().id());
    ASSERT_EQ(mmp->getable_count("queue1"), 7);
    ASSERT_EQ(mmp->waitack_count("queue1"), 0);
}

// 销毁测试
TEST(Messagetest, destroy_test)
{
    mmp->destroyQueueMessage("queue1");
}

int main(int argc, char *argv[])
{
    testing::AddGlobalTestEnvironment(new Messagetest);
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
